﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Page>" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<%@ Import Namespace="Buffalo.Administration.DAL" %>
<%@ Import Namespace="Buffalo.Administration.Enum" %>
<%@ Import Namespace="Buffalo.Lib.Common" %>
<%@ Import Namespace="Buffalo.Tourism.DAL" %>
<%@ Import Namespace="Buffalo.Tourism.DAL.Models" %>
<%@ Import Namespace="Buffalo.Tourism.Enum" %>
<%@ Import Namespace="Buffalo.Web.Areas.Tourism" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>TourDetail</title>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                bool visible =false;
                bool hasEditPermission = new PermissionDAL().HasPermission(GeneralVar.gLoggedUserName, "TourInfo", enuPermissionType.Edit);
                if(hasEditPermission)
                {
                    string IDTourDetail = Util.ToSafeString(ViewData["IDTourDetail"]);
                    TourDetail tourDetail = new TourProgramDAL().GetTourDetail(IDTourDetail);

                    if (string.IsNullOrEmpty(tourDetail.LocationRoleName) || GeneralVar.gLoggedRoleName == tourDetail.LocationRoleName)
                    {
                        visible = true;
                    }
                    else
                    { 
                        Tour tour = new TourProgramDAL().GetTour( tourDetail.IDTour );
                        if(GeneralVar.gLoggedRoleName != tourDetail.LocationRoleName &&  tour.LocationRoleName == GeneralVar.gLoggedRoleName)
                            visible = true;
                    }
                }
                btnSave.Visible = visible;
                btnSaveAndNew.Visible = visible;
            }
        }
    </script>

    <script type="text/javascript">
        var dataChanged = false;
        var getID = function () {
            return (dsTourDetail.getCount() > 0 && !dsTourDetail.getAt(0).newRecord) ? dsTourDetail.getAt(0).id : "";
        };
        var hideTourDetailWindow = function () {
            parent.TourDetailEditorWindow.hide();
            if (dataChanged)
                parent.dsListTourDetail.reload();
        };

        var failureHandler = function (form, action) {
            var msg = "";
            if (action.failureType == "client" || (action.result && action.result.errors && action.result.errors.length > 0)) {
                msg = "Please check fields";
            } else if (action.result && action.result.extraParams.msg) {
                msg = action.result.extraParams.msg;
            } else if (action.response) {
                msg = action.response.responseText;
            }

            Ext.Msg.show({
                title: "Save Error",
                msg: msg,
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
        };
        var successHandlerSaveClose = function (form, action) {
            successHandlerSave(form, action);
            hideTourDetailWindow();
        }
        var successHandlerSave = function (form, action) {
            if (action.result && action.result.extraParams && action.result.extraParams.newID) {
                dsTourDetail.getAt(0).id = action.result.extraParams.newID;
                if (dsTourDetail.getAt(0).newRecord) {
                    delete dsTourDetail.getAt(0).newRecord;
                }
            } else {
                Ext.MessageBox.alert("Success", "Tour has been saved!");
            }

            dataChanged = true;

            if (action.options.params.setNew) {
                TourDetailForm.form.reset();
                dsTourDetail.removeAll();

                var rec = new dsTourDetail.recordType();
                rec.newRecord = true;
                dsTourDetail.add(rec);
            }
        };

        var tourLoaded = function (store, records) {
            if (records.length > 0) {
                TourDetailForm.form.loadRecord(records[0]);

            } else {
                TourDetailForm.form.reset();
            }

            TourTabPanel.el.unmask();
        };

    </script>
</head>
<body>
    <ext:ResourceManager ID="ResourceManager1" runat="server">
        <Listeners>
            <DocumentReady Handler="#{dsTourDetail}.load();" />
        </Listeners>
    </ext:ResourceManager>
    <ext:Store ID="dsTourDetail" runat="server" AutoLoad="false">
        <Proxy>
            <ext:HttpProxy Url="/TourProgram/GetTourDetail/" />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="ID" Root="data" TotalProperty="total">
                <Fields>
                    <ext:RecordField Name="ID" />
                    <ext:RecordField Name="IDTour" />
                    <ext:RecordField Name="TourDate" Type="Date" DateFormat="yyyy-MM-ddThh:mm:ss" />
                    <ext:RecordField Name="DayTime" />
                    <ext:RecordField Name="Program" />
                    <ext:RecordField Name="Location" />
                    <ext:RecordField Name="Program" />
                    <ext:RecordField Name="IDRestaurant" />
                    <ext:RecordField Name="RestaurantName" />
                    <ext:RecordField Name="RestaurantNote" />
                    <ext:RecordField Name="IDHotel" />
                    <ext:RecordField Name="HotelName" />
                    <ext:RecordField Name="HotelNote" />
                    <ext:RecordField Name="IDTourGuide" />
                    <ext:RecordField Name="TourGuideName" />
                    <ext:RecordField Name="TourGuideNote" />
                    <ext:RecordField Name="IDVehicle" />
                    <ext:RecordField Name="VehicleName" />
                    <ext:RecordField Name="VehicleNote" />
                    <ext:RecordField Name="IDPartner" />
                    <ext:RecordField Name="PartnerName" />
                    <ext:RecordField Name="PartnerNote" />
                    <ext:RecordField Name="AirTicketNote" />
                    <ext:RecordField Name="IDOrgUnit" />
                    <ext:RecordField Name="GuestNum" />
                    <ext:RecordField Name="RoomNum" />
                    <ext:RecordField Name="LocationRoleName" />
                    <ext:RecordField Name="Gift1" />
                    <ext:RecordField Name="Gift2" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <BaseParams>
            <ext:Parameter Name="IDTourDetail" Value='<%# ViewData["IDTourDetail"] %>' Mode="Value"
                AutoDataBind="true" />
        </BaseParams>
        <Listeners>
            <BeforeLoad Handler="#{TourTabPanel}.el.mask('Loading tour detail ...', 'x-mask-loading');" />
            <LoadException Handler="#{TourTabPanel}.el.unmask();" />
            <Load Fn="tourLoaded" />
        </Listeners>
    </ext:Store>
    <ext:Store ID="dsTourGuide" runat="server" AutoLoad="true">
        <Proxy>
            <ext:HttpProxy Url="/Tourism/TourGuide/GetListTourGuide/" />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="ID" Root="data" TotalProperty="total">
                <Fields>
                    <ext:RecordField Name="ID" />
                    <ext:RecordField Name="Name" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <BaseParams>
            <ext:Parameter Name="limit" Value="15" Mode="Raw" />
            <ext:Parameter Name="start" Value="0" Mode="Raw" />
            <ext:Parameter Name="dir" Value="ASC" Mode="Value" />
            <ext:Parameter Name="sort" Value="Name" Mode="Value" />
            <ext:Parameter Name="withDefaultItem" Value="true"  Mode="Value"/>            
        </BaseParams>
        <SortInfo Field="Name" Direction="ASC" />
    </ext:Store>

    <ext:Store ID="dsHotel" runat="server" AutoLoad="true">
        <Proxy>
            <ext:HttpProxy Url="/Tourism/Catalog/GetListHotel/" />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="ID" Root="data" TotalProperty="total">
                <Fields>
                    <ext:RecordField Name="ID" />
                    <ext:RecordField Name="Name" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <BaseParams>
            <ext:Parameter Name="limit" Value="15" Mode="Raw" />
            <ext:Parameter Name="start" Value="0" Mode="Raw" />
            <ext:Parameter Name="dir" Value="ASC" Mode="Value" />
            <ext:Parameter Name="sort" Value="Name" Mode="Value" />
        </BaseParams>
        <SortInfo Field="Name" Direction="ASC" />
    </ext:Store>
    <ext:Store ID="dsRestaurant" runat="server" AutoLoad="true">
        <Proxy>
            <ext:HttpProxy Url="/Tourism/Catalog/GetListRestaurant/" />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="ID" Root="data" TotalProperty="total">
                <Fields>
                    <ext:RecordField Name="ID" />
                    <ext:RecordField Name="Name" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <BaseParams>
            <ext:Parameter Name="limit" Value="15" Mode="Raw" />
            <ext:Parameter Name="start" Value="0" Mode="Raw" />
            <ext:Parameter Name="dir" Value="ASC" Mode="Value" />
            <ext:Parameter Name="sort" Value="Name" Mode="Value" />
            <ext:Parameter Name="withDefaultItem" Value="true"  Mode="Value"/>            
        </BaseParams>
        <SortInfo Field="Name" Direction="ASC" />
    </ext:Store>
    <ext:Store ID="dsVehicle" runat="server" AutoLoad="true">
        <Proxy>
            <ext:HttpProxy Url="/Tourism/Catalog/GetListVehicle/" />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="ID" Root="data" TotalProperty="total">
                <Fields>
                    <ext:RecordField Name="ID" />
                    <ext:RecordField Name="Name" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <BaseParams>
            <ext:Parameter Name="limit" Value="15" Mode="Raw" />
            <ext:Parameter Name="start" Value="0" Mode="Raw" />
            <ext:Parameter Name="dir" Value="ASC" Mode="Value" />
            <ext:Parameter Name="sort" Value="Name" Mode="Value" />
        </BaseParams>
        <SortInfo Field="Name" Direction="ASC" />
    </ext:Store>
    <ext:Store ID="dsPartner" runat="server" AutoLoad="true">
        <Proxy>
            <ext:HttpProxy Url="/Tourism/Catalog/GetListPartner/" />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="ID" Root="data" TotalProperty="total">
                <Fields>
                    <ext:RecordField Name="ID" />
                    <ext:RecordField Name="Name" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <BaseParams>
            <ext:Parameter Name="limit" Value="15" Mode="Raw" />
            <ext:Parameter Name="start" Value="0" Mode="Raw" />
            <ext:Parameter Name="dir" Value="ASC" Mode="Value" />
            <ext:Parameter Name="sort" Value="Name" Mode="Value" />
        </BaseParams>
        <SortInfo Field="Name" Direction="ASC" />
    </ext:Store>

    <ext:Store ID="dsRole" runat="server" AutoLoad="true">
        <Proxy>
            <ext:HttpProxy Url="/Tourism/Member/GetListRole/" />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="Name" Root="data" TotalProperty="total">
                <Fields>
                    <ext:RecordField Name="Name" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <BaseParams>
            <ext:Parameter Name="limit" Value="15" Mode="Raw" />
            <ext:Parameter Name="start" Value="0" Mode="Raw" />
            <ext:Parameter Name="dir" Value="ASC" Mode="Value" />
            <ext:Parameter Name="sort" Value="Name" Mode="Value" />
        </BaseParams>
        <SortInfo Field="Name" Direction="ASC" />
    </ext:Store>

    <ext:Viewport ID="mainViewPort" runat="server" Layout="fit">
        <Items>
            <ext:Panel ID="Panel1" runat="server" Border="false" Layout="fit">
                <TopBar>
                    <ext:Toolbar ID="Toolbar1" runat="server">
                        <Items>
                            <ext:Button ID="btnSave" runat="server" Text="Save" Icon="Disk">
                                <Listeners>
                                    <Click Handler="#{TourDetailForm}.form.submit({ waitMsg : 'Saving...', params : { id : getID() }, success : successHandlerSave, failure : failureHandler });" />
                                </Listeners>
                            </ext:Button>
                            <ext:Button ID="btnSaveAndNew" runat="server" Text="Save and Close" Icon="DiskBlack">
                                <Listeners>
                                    <Click Handler="#{TourDetailForm}.form.submit({ waitMsg : 'Saving...', params : { id : getID() }, success : successHandlerSaveClose, failure : failureHandler });" />
                                </Listeners>
                            </ext:Button>
                            <ext:Button ID="btnClose" runat="server" Text="Close" Icon="DoorOut">
                                <Listeners>
                                    <Click Handler="hideTourDetailWindow();" />
                                </Listeners>
                            </ext:Button>
                            <ext:ToolbarFill ID="ToolbarFill1" runat="server" />
                        </Items>
                    </ext:Toolbar>
                </TopBar>
                <Items>
                    <ext:TabPanel ID="TourTabPanel" runat="server" Border="false" LayoutOnTabChange="true">
                        <Items>
                            <ext:Panel ID="panTourInfo" runat="server" Title="Tour Info" Padding="6" Layout="fit">
                                <Items>
                                    <ext:FormPanel ID="TourDetailForm" runat="server" Border="false" Url="/TourProgram/SaveTourDetail/"
                                        LabelWidth="130">
                                        <Items>
                                            <ext:Container ID="Container1" runat="server" Flex="1" Layout="Column" Height="600">
                                                <Items>
                                                <ext:Container ID="Container2" runat="server" Layout="Form" ColumnWidth=".5">
                                                <Items>
                                                    <ext:DateField ID="TourDate" runat="server" FieldLabel="Tour Date" Width="300" Format="dd-MM-yyyy" ReadOnly="true" />
                                                    <ext:TextField ID="DayTime" runat="server" FieldLabel="Day Time" Width="300"  ReadOnly="true" />
                                                    <ext:TextField ID="Program" runat="server" FieldLabel="Program" Width="300" />
                                                    <ext:NumberField ID="GuestNum" runat="server" EnableKeyEvents="true"  FieldLabel="GuestNum"
                                                        DecimalPrecision="0" MaxLength="10"  AllowBlank="true">
                                                    </ext:NumberField>
                                                    <ext:NumberField ID="RoomNum" runat="server" EnableKeyEvents="true" FieldLabel="RoomNum"
                                                        DecimalPrecision="0" MaxLength="10"  AllowBlank="true">
                                                    </ext:NumberField>
                                                    <ext:ComboBox ID="LocationRoleName" runat="server" StoreID="dsRole" Editable="false" 
                                                        FieldLabel="Role" DisplayField="Name" ValueField="Name" TypeAhead="true"
                                                        Mode="Local" ForceSelection="true" Width="300" TriggerAction="All" 
                                                        SelectOnFocus="true">
                                                    </ext:ComboBox>
                                                    <ext:TextField ID="Gift1" runat="server" FieldLabel="SIMGift" Width="300" />
                                                 </Items>
                                                 </ext:Container>

                                                 <ext:Container ID="Container3" runat="server" Layout="Form" ColumnWidth=".5">
                                                 <Items>
                                                    <ext:Hidden ID="IDTourGuide" runat="server"  />
                                                    <ext:ComboBox ID="TourGuideName" runat="server" StoreID="dsTourGuide" Editable="false"
                                                        FieldLabel="Tour Guide" DisplayField="Name" ValueField="ID" TypeAhead="true" 
                                                        Mode="Local" ForceSelection="true" Width="300" TriggerAction="All" EmptyText="Select a tour guide..."
                                                        SelectOnFocus="true">
                                                        <Listeners>
                                                            <Select Handler="#{IDTourGuide}.setValue(#{TourGuideName}.getValue());" />                                                            
                                                        </Listeners>
                                                    </ext:ComboBox>
                                                    <ext:TextField ID="TourGuideNote" runat="server" FieldLabel="TourGuide Note" Width="300" />

                                                    <ext:Hidden ID="IDHotel" runat="server"  />
                                                    <ext:ComboBox ID="HotelName" runat="server" StoreID="dsHotel" Editable="false" 
                                                        FieldLabel="Hotel" DisplayField="Name" ValueField="ID" TypeAhead="true"
                                                        Mode="Local" ForceSelection="true" Width="300" TriggerAction="All" EmptyText="Select a Hotel..."
                                                        SelectOnFocus="true">
                                                        <Listeners>
                                                            <Select Handler="#{IDHotel}.setValue(#{HotelName}.getValue());" />                                                            
                                                        </Listeners>
                                                    </ext:ComboBox>
                                                    <ext:TextField ID="HotelNote" runat="server" FieldLabel="Hotel Note" Width="300" />

                                                    <ext:Hidden ID="IDVehicle" runat="server"  />
                                                    <ext:ComboBox ID="VehicleName" runat="server" StoreID="dsVehicle" Editable="false" 
                                                        FieldLabel="Vehicle" DisplayField="Name" ValueField="ID" TypeAhead="true"
                                                        Mode="Local" ForceSelection="true" Width="300" TriggerAction="All" EmptyText="Select a Vehicle..."
                                                        SelectOnFocus="true">
                                                        <Listeners>
                                                            <Select Handler="#{IDVehicle}.setValue(#{VehicleName}.getValue());" />                                                            
                                                        </Listeners>
                                                    </ext:ComboBox>
                                                    <ext:TextField ID="VehicleNote" runat="server" FieldLabel="Vehicle Note" Width="300" />

                                                    <ext:Hidden ID="IDRestaurant" runat="server"  />
                                                    <ext:ComboBox ID="RestaurantName" runat="server" StoreID="dsRestaurant" Editable="false" 
                                                        FieldLabel="Restaurant" DisplayField="Name" ValueField="ID" TypeAhead="true"
                                                        Mode="Local" ForceSelection="true" Width="300" TriggerAction="All" EmptyText="Select a Restaurant..."
                                                        SelectOnFocus="true">
                                                        <Listeners>
                                                            <Select Handler="#{IDRestaurant}.setValue(#{RestaurantName}.getValue());" />                                                            
                                                        </Listeners>
                                                    </ext:ComboBox>
                                                    <ext:TextField ID="RestaurantNote" runat="server" FieldLabel="Restaurant Note" Width="300" />

                                                    <ext:Hidden ID="IDPartner" runat="server"  />
                                                    <ext:ComboBox ID="PartnerName" runat="server" StoreID="dsPartner" Editable="false" 
                                                        FieldLabel="OtherService" DisplayField="Name" ValueField="ID" TypeAhead="true"
                                                        Mode="Local" ForceSelection="true" Width="300" TriggerAction="All" EmptyText="Select a service..."
                                                        SelectOnFocus="true">
                                                        <Listeners>
                                                            <Select Handler="#{IDPartner}.setValue(#{PartnerName}.getValue());" />                                                            
                                                        </Listeners>
                                                    </ext:ComboBox>
                                                    <ext:TextField ID="PartnerNote" runat="server" FieldLabel="OtherServiceNote" Width="300" />

<%--                                                    <ext:Hidden ID="IDOrgUnit" runat="server"  />
                                                    <ext:ComboBox ID="OrgUnitName" runat="server" StoreID="dsOrgUnit" Editable="false" 
                                                        FieldLabel="OrgUnit" DisplayField="Name" ValueField="ID" TypeAhead="true"
                                                        Mode="Local" ForceSelection="true" Width="300" TriggerAction="All" EmptyText="Select a OrgUnit..."
                                                        SelectOnFocus="true">
                                                        <Listeners>
                                                            <Select Handler="#{IDOrgUnit}.setValue(#{OrgUnitName}.getValue());" />                                                            
                                                        </Listeners>
                                                    </ext:ComboBox>--%>
                                                    </Items>
                                                    </ext:Container>
                                                </Items>
                                            </ext:Container>
                                        </Items>
                                        <BaseParams>
                                            <ext:Parameter Name="IDTourDetail" Value='<%# ViewData["IDTourDetail"] %>' Mode="Value"
                                                AutoDataBind="true" />
                                        </BaseParams>
                                    </ext:FormPanel>
                                </Items>
                            </ext:Panel>
                        </Items>
                    </ext:TabPanel>
                </Items>
            </ext:Panel>
        </Items>
    </ext:Viewport>
</body>
</html>
